local setmetatable, getmetatable, type =
      setmetatable, getmetatable, type
module('apr.oo')

--- Simplify simple class-like behavior.  Take a constructor function and generate a class table with a .new() function for calling the constructor and creating instances
-- @param an optional constructor function, receiving "self" as the first argument, then all arguments passed to Class.new()
-- @return the class table
function Class(init)
    
    local class = {}
    class.__index = class
    class.new = function(...)
        local instance = {}
        setmetatable(instance, class)
        if init then
            local results = {init(instance, ...)}
            --constructors that return nil, error (a common error throwing mechanism) will cause .new() to return the same thing
            if #results > 0 and results[1] == nil then return unpack(results) end
        end
        return instance
    end
    
    return class
end

--- Return true if the metatable for object matches class
-- @param obj the instance table to check
-- @param class the class table (metatable) to check against
-- @return boolean test result
function isinstance(obj, class)
    return getmetatable(obj) == class
end
